function theisiPrepare()

    clc 
    close all
    bands = 224
    %%Helper.loadAllData(bands)  %% Step1：把原始数据导出为mat文件
    
    %% 7月26日的数据
    datapath = 'F:\12份土壤\sun\data';%% 反射率提取后的 Mat数据文件
    dataSrcInput = 'F:\12份土壤\sun\input';%% 原始光谱反射率TXT文件
    fileSavePath = 'F:\12份土壤\sun\output';%% R2，RMSE结果输出文件
    fileSavePath = 'F:\12份土壤\sun\thesis';%% R2，RMSE结果输出文件
    %% 7月28日的数据
% %     datapath = 'F:\12份土壤\sun\matlab\data\20220728';%% 反射率提取后的 Mat数据文件
% %     dataSrcInput = 'F:\12份土壤\sun\matlab\input\20220728';%% 原始光谱反射率TXT文件
% %     fileSavePath = 'F:\12份土壤\sunmatlab\output';%% R2，RMSE结果输出文件
    bandFile = 'F:\12份土壤\sun\thesis\bands.txt';
    dataExcelFile = 'F:\12份土壤\sun\合并处理后的sheets.xlsx';
    
    args.bands = bands;
    args.removeHead = 0;%去掉前10    
    args.removeHead = 5;%去掉前10
    args.removeTail = 0;
    args.removeTail = 9;%%去掉后10
    args.containMetal = false;
    args.containMetal = true;%%保留金属
    args.dataSrcInput = dataSrcInput;%%Step 1
    
    args.fileSavePath = fileSavePath;
    args.dataOutputPath = fileSavePath;
    args.bandFile = bandFile;
    args.dataExcelFile = dataExcelFile;
    
    isBatch = true
    isBatch = false
    isBatch = true
    
    if isBatch == true
        DoLoadAllData(datapath, args)    
        return;
    end  
    
end


function DoLoadAllData(datapath, args)
    step1 = 1;      step2 = 2;     step3 = 4;       step4 = 8;
    step5 = 16;     step6 = 32;    step7 = 128;     step8 = 256;
    step9 = 512;    step10 = 1024; step11 = 2048;
    step12 = step11 * 2;    step13 = step12 * 2;    step14 = step13 * 2;
    step15 = step14 * 2;    step16 = step15 * 2;    step17 = step16 * 2;
    step18 = step17 * 2;    step19 = step18 * 2;    step20 = step19 * 2;
    step21 = step20 * 2;    step22 = step21 * 2;    step23 = step22 * 2;
    step24 = step23 * 2;    step25 = step24 * 2;    step26 = step25 * 2;
    step27 = step26 * 2;    step28 = step27 * 2;    step29 = step28 * 2;
    step30 = step29 * 2;    step31 = step30 * 2;    step32 = step31 * 2;
    step33 = step32 * 2;    step34 = step33 * 2;    step35 = step34 * 2;
    step = 0;
%     step = step + step1;%%Step1：把原始数据导出为mat文件
    step = step + step2;%%Step2：加载数据，去掉前5后15
% %     step = step + step3;% 计算波段与测量值的协方差
    
    
%     step = step + step4;% 去除PLSRegression    在5种预处理的数据
%     step = step + step5;%%SVR
%     step = step + step6;%%决策树 与随机森林
%     step = step + step7;%%LSBoost
    
%     step = step + step8;%%画出不同元素的多种回归方法
    
%     step = step + step22;%%读取所有数据，画出RPD，RMSE，MAPE三个图
%    step = step + step23;%%整理每个元素的RF的预处理+特征+模型的数据
%    step = step + step24;%%读取所有数据，画出RPD，RMSE，MAPE三个图

     
%     step = step + step9;%%改CR,       
%     step = step + step10;%%SG平滑
%     step = step + step11;%%MSC
%     step = step + step12;%%SNV
%     step = step + step13;%%MA
%     step = step + step14;%%一阶导数
%     step = step + step15;%%二阶导数



%      step = step + step26% 整理每个元素的 多预处理+多模型的数据
%      step = step + step27%
%       step = step + step28% 
%      step = step + step29%% 
%       step = step + step30%% 
% 
%     step = step + step31;%% 
%     step = step + step32;%% 

      step = step + step33; %% 显示最优波长
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %     
     bands = args.bands;
     fileSavePath = args.fileSavePath;
     dataPath = args.fileSavePath;
     if bitand(step, step1)     
        disp('Step 1. ...............');
        
        ret = Helper.loadExcelAllData(args.dataExcelFile);
        bandX = load(args.bandFile, '-ascii');
        ThesisHelper.plot24Curve(ret', args.removeHead, args.removeTail, bandX); 
        %% 从原始全波段数据进行绘制曲线
     end
     
     %% 加载全光谱数据
     if bitand(step, step2)     
        disp('Step 2. ...............');
% %         loadAllData(bands)
%         [y_true, xx]  = loadExcelData(datapath);
        data = load(fullfile(args.fileSavePath, 'proc_data.mat'));
        y_true = data.y_true;
        y_data = y_true;
        x_data = data.x_proc;
        x_data = x_data';
        
        corrcoef(y_true)
%         plotmatrix(y_true')
        bandX = load(args.bandFile, '-ascii');     

        bandX = bandX(args.removeHead + 1 : end);
        lenB = length(bandX);
        bandX = bandX( 1 : lenB - args.removeTail);
      
        %%后面都要改
%         ThesisHelper.plotPair(x_data);%% 设置removeHead=0， removeTail=0；
%         
%         Helper.plotPair(x_data);
     end    
     
     %% 计算波段与测量值的协方差
     if bitand(step, step3)     
        disp('Step 3. ...............');
        
        Helper.showCorrelationEachBand(x_data, y_true', bandX)
        
        f = ThesisHelper.GetAFrame();
        im = frame2im(f);
        saveas(gca, fullfile(args.fileSavePath, '波段与测量值相关系数.emf'))
        savefig(fullfile(args.fileSavePath, '波段与测量值相关系数.fig'))

     end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %    c
     %%%%%%%进行CR滤波  与绘图
     bShow = false
%      bShow = true
     if bitand(step, step9)     
        disp('Step 9. CR滤波...............');

        x_data = PreHelper.DoCR(x_data, bShow);%%x_data, 204*24
        
        saveName = 'F:\12份土壤\sun\thesis\CR.emf';
        ThesisHelper.plot24CurveNoHT(x_data, saveName, bandX)
     end
     %%%%%%%进行SG滤波  与绘图
     if bitand(step, step10)     
        disp('Step 10. SG滤波...............');

        x_data = PreHelper.DoSG(x_data);%%x_data, 204*24
        
        saveName = 'F:\12份土壤\sun\thesis\SG.emf';
        ThesisHelper.plot24CurveNoHT(x_data, saveName)
     end
     %%%%%%%进行MSC预处理 与绘图
     if bitand(step, step11)     
        disp('Step 11. MSC预处理...............');
        x_data = PreHelper.DoMSC(x_data);%%x_data, 204*24
        
        saveName = 'F:\12份土壤\sun\thesis\MSC.emf';
        ThesisHelper.plot24CurveNoHT(x_data, saveName)
     end
     %%%%%%%进行SNV预处理 与绘图  
     if bitand(step, step12)     
        disp('Step 12. SNV预处理...............');
        x_data = PreHelper.DoSNV(x_data);%%x_data, 204*24 
        
        saveName = 'F:\12份土壤\sun\thesis\SNV.emf';
        ThesisHelper.plot24CurveNoHT(x_data, saveName)
     end
     %%%%%%%进行1阶导数滤波   与绘图  
     if bitand(step, step14)     
        disp('Step 14. 1阶导数滤波...............');
        bShow = true
        bShow = false
%         x_data = PreHelper.DoMA(x_data, 3);%%x_data, 204*24
        x_data = PreHelper.DoDiff(x_data, 1, bShow);%%x_data, 204*24  
        
        saveName = 'F:\12份土壤\sun\thesis\FD.emf';
        ThesisHelper.plot24CurveNoHT(x_data, saveName)
     end 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %      
     if bitand(step, step4)     
        disp('Step 4. 取出PLSR的各个预处理数据...............');

        dataPath = 'F:\12份土壤\sun\thesis\'
        dataFile = 'all-pre-PLSR.txt';
        dataIncputFile = [dataPath, dataFile]

        dataSaveFile = 'thesis-all-pre-PLSR.txt';
        dataOutputFile = [dataPath, dataSaveFile]

        ThesisHelper.SaveDataToFormatFile(dataIncputFile, dataOutputFile);     
     end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %      
     if bitand(step, step5)     
        disp('Step 5. 取出SVR的各个预处理数据...............');

        dataPath = 'F:\12份土壤\sun\thesis\'
        dataFile = 'all-pre-SVR.txt';
        dataIncputFile = [dataPath, dataFile]

        dataSaveFile = 'thesis-all-pre-SVR.txt';
        dataOutputFile = [dataPath, dataSaveFile]

        ThesisHelper.SaveDataToFormatFile(dataIncputFile, dataOutputFile);     
     end     
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %      
     if bitand(step, step6)     
        disp('Step 6. 取出RF的各个预处理数据...............');

        dataPath = 'F:\12份土壤\sun\thesis\'
        dataFile = 'all-pre-RF.txt';
        dataIncputFile = [dataPath, dataFile]

        dataSaveFile = 'thesis-all-pre-RF.txt';
        dataOutputFile = [dataPath, dataSaveFile]

        ThesisHelper.SaveDataToFormatFile(dataIncputFile, dataOutputFile);     
     end         
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %      
     if bitand(step, step7)     
        disp('Step 7. 取出LSBoost的各个预处理数据...............');

        dataPath = 'F:\12份土壤\sun\thesis\'
        dataFile = 'all-pre-LSB.txt';
        dataIncputFile = [dataPath, dataFile]

        dataSaveFile = 'thesis-all-pre-LSB.txt';
        dataOutputFile = [dataPath, dataSaveFile]

        ThesisHelper.SaveDataToFormatFile(dataIncputFile, dataOutputFile);     
     end           
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %      
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
     if bitand(step, step8)     
        disp('Step 8. 画出不同元素的不同回归方法的R2,RMSE,MAPE.............');

        dataPath = 'F:\12份土壤\sun\thesis\'
        dataFile = cell(4, 1);
        dataFile{1} = 'all-pre-PLSR.txt';
        dataFile{2} = 'all-pre-SVR.txt';
        dataFile{3} = 'all-pre-RF.txt';
        dataFile{4} = 'all-pre-LSB.txt';
        
        data = cell(4, 1);
        for i = 1 : length(dataFile)
            dataIncputFile = [dataPath, dataFile{i}];
            
            fileID = fopen(dataIncputFile);
            tmp = textscan(fileID, '%f');
            tmp = reshape(tmp{1}, [10, 21])'; 
            data{i} = tmp;
            fclose(fileID);   
        end
        dd = cell2mat(data);
        
        fileSave = cell(3, 1);
        fileSave{1} = 'fig3_AN_1.emf';
        fileSave{2} = 'fig3_AP_2.emf';
        fileSave{3} = 'fig3_AK_3.emf';

        
        for i = 1 : length(fileSave)
            fileSave{i} = [dataPath, fileSave{i}];
        end
        ThesisHelper.DrawMetalsFigure(dd, fileSave);
     end        
     
     
     if bitand(step, step22)
         disp('Step 22. 读取所有数据，画出RPD，RMSE，MAPE三个图.............');
         
         dataPath = 'F:\12份土壤\sun\thesis\'
         dataFile = 'all-data.txt';
         dataInputFile = [dataPath, dataFile];
         data = load(dataInputFile, '-ascii');
         
         fileSave = fullfile(dataPath, 'table-all-data.txt');
         
         ThesisHelper.DrawMetricFigures(data, fileSave);
     end
     
     if bitand(step, step23)
        disp('Step 23. 整理每个元素的RF的预处理+特征+模型的数据............');

        dataFile = 'rf-pre-variable-all-data.txt';%%由excel文件copy
        dataFile = 'svr-pre-variable-all-data.txt';%%由excel文件copy
        dataInputFile = fullfile(args.fileSavePath, dataFile);
        data = load(dataInputFile, '-ascii');
         
        fileSave = cell(3, 1);
        fileSave{1} = 'table_AN_all.txt';
        fileSave{2} = 'table_AP_all.txt';
        fileSave{3} = 'table_AK_all.txt';
        
        for i = 1 : length(fileSave)
            fileSave{i} = fullfile(fileSavePath, fileSave{i});
        end
         
        ThesisHelper.BuildMetricTables(data, fileSave);
%         ThesisHelper.DrawMetricTables(data, fileSave);
     end
     if bitand(step, step24)
         disp('Step 24. 组织特征选择的结果数据 要放入表格............');
         %% 文件要先通过 批处理合并后处理
         
         dataPath = 'F:\12份土壤\sun\thesis\'
         dataFile = '=fea-metals-all.txt';
         dataInputFile = [dataPath, dataFile];
         data = load(dataInputFile, '-ascii');
         fileSave = [dataPath, 'table-all-features.txt'];
         
         ThesisHelper.GetFeatureData(data, fileSave);
     end
     
     
     if bitand(step, step26)
        disp('Step 26. 整理每个元素的 多预处理+多模型的数据............');

        dataFile = 'model-pre-all-data.txt';%%由excel文件copy，不包括训练集，所有模型
        dataInputFile = fullfile(args.fileSavePath, dataFile);
        data = load(dataInputFile, '-ascii');
         
        fileSave = cell(3, 1);
        fileSave{1} = 'table_AN_all.txt';
        fileSave{2} = 'table_AP_all.txt';
        fileSave{3} = 'table_AK_all.txt';
        
        for i = 1 : length(fileSave)
            fileSave{i} = fullfile(fileSavePath, fileSave{i});
        end
         
        ThesisHelper.DrawMetricTables(data, fileSave);%%保存数据到文件
     end
     
     if bitand(step, step27)%%先运行step26整理完数据
         disp('Step 27. 绘制Bar图............');
         
         dataPath = 'F:\12份土壤\sun\thesis\'
         
         fileLoad = cell(3, 1);
         fileLoad{1} = 'table_AN_all.txt';%%增加全光谱1行
         fileLoad{2} = 'table_AP_all.txt';
         fileLoad{3} = 'table_AK_all.txt';

        
         fileSave = cell(3,1);
         fileSave{1} = 'fig_AN_all';%%增加全光谱1行
         fileSave{2} = 'fig_AP_all';
         fileSave{3} = 'fig_AK_all';
         
         for i = 1 : length(fileLoad)
             fileSave{i} = [dataPath, fileSave{i}];
             fileLoad{i} = [dataPath, fileLoad{i}];
         end
         
         fileDiff = cell(3,1);
         fileDiff{1} = '_r2.emf';
         fileDiff{2} = '_rmse.emf';
         fileDiff{3} = '_mape.emf';
         
         isClose = true;
%          isClose = false;%%是否关闭打开窗口
%          ThesisHelper.BuildMetricTables2(data, fileSave);
         ThesisHelper.DrawBarFigure(fileLoad, fileSave, fileDiff, isClose);%%增加全光谱
     end
     
     if bitand(step, step33)%%从文件中读取最优波长
        setting = [1, 3, 3;
           1, 3, 6;
           1, 3, 1];
        bandX = load(args.bandFile, '-ascii');
        bandX = bandX(args.removeHead + 1 : end);
        lenB = length(bandX);
        bandX = bandX( 1 : lenB - args.removeTail);
        
        
        fileSave = cell(3, 1);
        fileSave{1} =  'waves-opt-metal-1.txt';
        fileSave{2} =  'waves-opt-metal-2.txt';
        fileSave{3} =  'waves-opt-metal-3.txt';

        for i = 1 : 3
            fileSave{i} = fullfile(fileSavePath, fileSave{i});
        end
        
        wave = 20;

        for metal = 1 : size(setting, 1)            
            ret = load(fileSave{metal}, '-ascii');
            fsss{metal} = ret;
            bandX(ret)
        end
        bandBest = FeaSelect.plotBandsEachMetal2(x_data', y_data, fsss, wave, setting, bandX);
%         bandBest = FeaSelect.plotBandsEachMetal(x_data', y_data, fsss, wave, setting, bandX);

        fileSave = fullfile(args.fileSavePath, 'selectedWavelength.txt');
        save(fileSave, '-ascii', '-tabs', 'bandBest');
    end
     
     
     disp('All done');
end

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % Step2:  加载化学检测数据，加载mat波谱数据
function [y_true, xx] = loadExcelData(datapath)
    fileName = 'data.txt';
    fileFullName = fullfile(datapath, fileName);
    
    fid = fopen(fileFullName);
    lines = textscan(fid,'%s %s %s %f %s %s', 'Delimiter','\t');
    data = lines{4};
    len = length(data);
    y_true = reshape(data, len/12,12);   
    fclose(fid)
    
    imdjson = imageDatastore(datapath, 'FileExtensions',{'.mat'}, 'IncludeSubfolders', false);
    xx = cell(length(imdjson.Files), 1);
    for i = 1 : length(imdjson.Files)  
        fileName = imdjson.Files{i};
        
        [~, fName, ~] = fileparts(fileName);
        lines = textscan(fName, '%d_%d');
        data = load(fileName);
        roi = data.roi;
        stats = roi.stats;
        
        first = lines{1};
        second = lines{2};
%         kk = (first - 1) * 2 + 1 + second - 1;
        kk = (second - 1) * 12 + 1 + first - 1;
        xx{kk} = stats;
    end
end

% % Step1：把原始数据导出为mat文件
function loadAllData(datapath, bands, isRefl)
    

end

